package com.forward.uploadapp.util;

import android.os.Environment;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class FileHelper {
    public static String BASE_PATH =
            Environment.getExternalStorageDirectory().toString();

    /**
     * 创建目录
     *
     * @param filePath 目录
     */
    public static boolean mkdir(String filePath) {
        try {
            if (TextUtils.isEmpty(filePath)) {

            } else {
                File file = new File(filePath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean mkAppDir(String filePath) {
        return mkdir(BASE_PATH + "/Download/output" + filePath);
    }

    /**
     * 创建文件
     *
     * @param filePath
     * @return
     */
    public static boolean createFile(String filePath) {
        try {
            if (!TextUtils.isEmpty(filePath)) {
                File file = new File(filePath);
                if (!file.exists() && mkdir(file.getParent())) {
                    file.createNewFile();
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取一个目录下所有的文件
     *
     * @param filePath
     * @return
     */
    public static List<File> getFileList(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return null;
        }
        File file = new File(filePath);
        if (!file.exists() || file.isFile()) {
            return null;
        }
        return Arrays.asList(file.listFiles());
    }

    /**
     * 将字符串写入文件
     *
     * @param text
     * @param fileName
     */
    public static boolean writeFile(String text, String fileName) {
        try {
            String filePath = BASE_PATH + "/Download/output" + fileName;
            File file = new File(filePath);

            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }

            FileOutputStream f = new FileOutputStream(filePath);
            f.write(text.getBytes());
            f.close();
            return true;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 将字符串写入文件
     *
     * @param filePath
     */
    public static String readFile(String filePath) {
        return readFile(new File(filePath));
    }

    /**
     * 从文件里独处字符窜
     *
     * @param file
     */
    public static String readFile(File file) {
        try {
            if (!file.exists() || file.isDirectory()) {
                return null;
            }
            byte[] bytes = new byte[1024];
            int len = 0;
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            StringBuffer buffer = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
            }
            reader.close();
            return buffer.toString();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}